package com.bc.bczbweb.controller.Ajax;

import com.bc.bcapi.service.T_SequenceService;
import com.bc.commoncore.security.AES;
import com.bc.commoncore.util.DateUtil;
import com.bc.commonentity.common.JsonResult;
import com.bc.bcapi.service.T_Mnguser_infoService;
import com.bc.bcmodel.domain.T_Mnguser_infoModel;
import com.bc.commonentity.enums.SeqNameEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.POST;
import java.math.BigInteger;

@RestController
@RequestMapping("ajax/t_mnguser_info")
public class AjaxT_Mnguser_infoController {
    @Autowired
    T_Mnguser_infoService t_Mnguser_infoService;
    @Autowired
    T_SequenceService t_SequenceService;

    @POST
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public JsonResult add(T_Mnguser_infoModel req) {
        JsonResult js = new JsonResult();

        if (req.getPassword() != null && !req.getPassword().equals("")) {
            req.setPassword(AES.aesEncrypt(req.getPassword()));
        }
        if (!req.getUser_id().equals("")) {
            req.setModify_time(DateUtil.getToday());
            T_Mnguser_infoModel chkCount = new T_Mnguser_infoModel();
            chkCount.setLogin_name(req.getLogin_name());
            long resultCount = t_Mnguser_infoService.count(chkCount);
            if (resultCount >= 2) {
                js.setCode(2);
                return js;
            }
            t_Mnguser_infoService.update(req);
        } else {
            T_Mnguser_infoModel chkCount = new T_Mnguser_infoModel();
            chkCount.setLogin_name(req.getLogin_name());
            long resultCount = t_Mnguser_infoService.count(chkCount);
            if (resultCount >= 1) {
                js.setCode(2);
                return js;
            }
            req.setUser_id(t_SequenceService.genSequence(SeqNameEnum.MANAGE_USERINFO_SEQ));
            t_Mnguser_infoService.insert(req);
        }
        return js;
    }

    @RequestMapping(value = "del", method = RequestMethod.POST)
    public JsonResult del(String user_id) {
        JsonResult js = new JsonResult();
        T_Mnguser_infoModel model = new T_Mnguser_infoModel();
        model.setUser_id(user_id);
        T_Mnguser_infoModel result = t_Mnguser_infoService.findByNamedParam(model);

        model = new T_Mnguser_infoModel();
        model.setUser_id(user_id);
        String status = "1";
        if (result.getStatus().equals("1")) {
            status = "0";
        }
        model.setStatus(status);
        t_Mnguser_infoService.update(model);
        return js;
    }
}